local skels = {}

local skel = fk.CreateSkill {
  name = "rmt__hebian",
  tags = {Skill.Switch},
}

skel:addEffect("viewas", {
  anim_type = "switch",
  pattern = "slash,peach",
  handly_pile = true,
  prompt = function (self, player, selected_cards, selected)
    return "#rmt__hebian-"..player:getSwitchSkillState(self.name, false, true)
  end,
  card_filter = function (self, player, to_select, selected)
    local n = player:getSwitchSkillState(self.name) == fk.SwitchYang and 1 or 2
    return #selected < n
  end,
  view_as = function (self, player, cards)
    local yang = player:getSwitchSkillState(self.name) == fk.SwitchYang
    if #cards ~= (yang and 1 or 2) then return nil end
    local c = Fk:cloneCard(yang and "peach" or "slash")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  after_use = function (self, player, use)
    local room = player.room
    local tos = table.simpleClone(use.tos)
    if #tos == 0 then return end
    room:sortByAction(tos)
    if use.card.trueName == "slash" then
      for _, to in ipairs(tos) do
        if not to.dead and not player.dead then
          local cards = table.connect(player:getHandlyIds(), player:getCardIds("e")) -- 能用木马牌
          if #cards < 2 then break end
          if room:askToSkillInvoke(to, {skill_name =  self.name, prompt = "#rmt__hebian-ask-slash:"..player.id}) then
            room:doIndicate(to, {player})
            local vs_skill = Fk.skills["rmt__hebian_slash"]---@type ViewAsSkill
            local _, dat = room:askToUseActiveSkill(player, {
              skill_name =  "rmt__hebian_slash", prompt =  "#rmt__hebian-slash",
              cancelable = false, extra_data = {bypass_times = true}
            })
            if not dat then
              -- 烧条默认值
              local function getDefaultData()
                for i = 1, #cards do
                  for j = i + 1, #cards do
                    local card = vs_skill:viewAs(player, {cards[i], cards[j]})
                    if card and not player:prohibitUse(card) then
                      local tars = card:getDefaultTarget(player, {bypass_times = true})
                      if #tars > 0 then
                        return {targets = tars, cards = {cards[i], cards[j]}}
                      end
                    end
                  end
                end
              end
              dat = getDefaultData()
            end
            if dat then
              local card = vs_skill:viewAs(player, dat.cards)
              if card then
                room:useCard{
                  from = player,
                  tos = dat.targets,
                  card = card,
                  extraUse = true,
                }
              end
            end
          end
        end
      end
    elseif use.card.trueName == "peach" then
      for _, to in ipairs(tos) do
        if not to.dead and to:isWounded() then
          local vs_skill = Fk.skills["rmt__hebian_peach"]---@type ViewAsSkill
          local _, dat = room:askToUseActiveSkill(to, {skill_name =  "rmt__hebian_peach", prompt = "#rmt__hebian-peach"})
          if dat then
            local card = vs_skill:viewAs(to, dat.cards)
            if card then
              room:useCard{
                from = to,
                tos = dat.targets,
                card = card,
                extraUse = true,
              }
            end
          end
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return true
  end,
  enabled_at_response = function (self, player, response)
    if not response then
      local yang = player:getSwitchSkillState(self.name) == fk.SwitchYang
      local name = yang and "peach" or "slash"
      return Exppattern:Parse(Fk.currentResponsePattern):matchExp(name)
      and not player:prohibitUse(Fk:cloneCard(name))
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__hebian"] = "阖变",
  [":rmt__hebian"] = "转换技，①你可以将一张牌当【桃】使用，然后目标也可如此做。②你可以将两张牌当【杀】使用，然后目标可令你再如此做。",

  [":rmt__hebian_yang"] = "转换技，<font color=\"#E0DB2F\">①你可以将一张牌当【桃】使用，然后目标也可如此做。</font>②你可以将两张牌当【杀】使用，然后目标可令你再如此做。",
  [":rmt__hebian_yin"] = "转换技，①你可以将一张牌当【桃】使用，然后目标也可如此做。<font color=\"#E0DB2F\">②你可以将两张牌当【杀】使用，然后目标可令你再如此做。</font>",

  ["#rmt__hebian-yin"] = "将两张牌当【杀】使用，目标可令你再如此做",
  ["#rmt__hebian-yang"] = "将一张牌当【桃】使用，然后目标也可如此做",
  ["rmt__hebian_peach"] = "阖变[桃]",
  ["rmt__hebian_slash"] = "阖变[杀]",
  ["#rmt__hebian-peach"] = "阖变：你可以将一张牌当【桃】使用",
  ["#rmt__hebian-slash"] = "阖变：你须将两张牌当【杀】使用！",
  ["#rmt__hebian-ask-slash"] = "阖变：你可以令 %src 须将两张牌当【杀】使用",
}

local skel2 = fk.CreateSkill { name = "rmt__hebian_peach" }

skel2:addEffect("viewas", {
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected < 1
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("peach")
    c.skillName = "rmt__hebian"
    c:addSubcards(cards)
    return c
  end,
})

local skel3 = fk.CreateSkill { name = "rmt__hebian_slash" }

skel3:addEffect("viewas", {
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 then return nil end
    local c = Fk:cloneCard("slash")
    c.skillName = "rmt__hebian"
    c:addSubcards(cards)
    return c
  end,
})


table.insert(skels, skel)
table.insert(skels, skel2)
table.insert(skels, skel3)

return skels
